//index.js
//获取应用实例
const app = getApp()
const host = require('../../config').host
const new_host = require('../../config').new_host
var url = ""
var article_types = ""

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
       url: '../logs/logs'
    })
  },
  method: function(){
    
    // console.log(curl);
    if(article_types == 1){
      var curl = new_host+'/uploads/other/石油石化企业管理现代化优秀论文评审办法文件.pdf'
    }else if(article_types == 2){
      var curl = new_host+'/uploads/other/石油石化企业管理现代化优秀著作评审办法文件.pdf'
    }else if(article_types == 3){
      var curl = new_host+'/uploads/other/石油石化企业管理现代化优秀成果评审办法文件.pdf'
    }
      wx.showLoading({
        title: '加载中',
        mask:true,
      })
      wx.downloadFile({
      // 示例 url，并非真实存在
      // url: 'https://newtest.shouwangkeji.com/uploads/20200314/%E7%AD%89%E7%AD%89.pdf',
      url: curl,
      success: function (res) {
        if (res.statusCode === 200) {
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            showMenu: true,
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        }
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      }
    })
  },
  letter: function(){
    
    // console.log(curl);
    if(article_types == 1){
      var curl = new_host+'/uploads/other/给评委的信-论文.docx'
    }else if(article_types == 2){
      var curl = new_host+'/uploads/other/给评委的信-著作.docx'
    }else if(article_types == 3){
      var curl = new_host+'/uploads/other/给评委的信-成果.docx'
    }
      wx.showLoading({
        title: '加载中',
        mask:true,
      })
      wx.downloadFile({
      // 示例 url，并非真实存在
      // url: 'https://newtest.shouwangkeji.com/uploads/20200314/%E7%AD%89%E7%AD%89.pdf',
      url: curl,
      success: function (res) {
        if (res.statusCode === 200) {
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            showMenu: true,
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        }
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      }
    })
  },
  files: function(){
    var curl = new_host+url
    console.log(curl);
      wx.showLoading({
        title: '加载中',
        mask:true,
      })
      wx.downloadFile({
      // 示例 url，并非真实存在
      // url: 'https://newtest.shouwangkeji.com/uploads/20200314/%E7%AD%89%E7%AD%89.pdf',
      url: curl,
      success: function (res) {
        if (res.statusCode === 200) {
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            showMenu: true,
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        }
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      }
    })
  },
  score: function(e){
    // console.log(e.currentTarget.dataset.article_type)
    wx.navigateTo({
      url: `./score?id=${e.currentTarget.dataset.id}&article_type=${e.currentTarget.dataset.article_type}`
    })
  },
  onLoad: function (params) {
  
    var book_id = params.id
    this.readFile(book_id)
  },
  readFile: function(book_id){
    var that = this
    wx.request({
      url: host+'api/expertreview/readFile',
      data:{expert_id:app.globalData.id,book_id:book_id},
      success: function(res){
        console.log(res.data.book_id.article_type)
        url = res.data.book_id.url
        article_types = res.data.book_id.article_type
        that.setData({
          readFile: res.data.book_id
        })
        that.setData({
          nicknames: res.data.nicknames
        })
        that.setData({
          stop_time: res.data.stop_time
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
